/**************************************************************

Copyright Astrium Services GEO-Information / Spot Image
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 ***************************************************************/
package com.spotimage.catalog.client.ui;

import java.util.Date;

import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.LatLngBounds;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.spotimage.catalog.client.ajax.Requester;
import com.spotimage.catalog.client.event.SearchResultEvent;
import com.spotimage.catalog.client.ui.tab.SearchTab;

/**
 * Search scenes button.
 */
public class SearchButton extends IButton {
	
	/**
	 * Set UI.
	 * @param searchTab
	 */
	public SearchButton(final SearchTab searchTab, final MapPanel mapPanel) {
		setTitle("Search catalog");
		setAlign(Alignment.CENTER);
		addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (!mapPanel.isRoiSet()) {
					SC.warn("You need to select a region of interest before search.<br><br>" +
							"To draw the region of interest on the map, " +
							"left-click on the map, move your mouse and then click on the map to stop drawing.");
					return;
				} else {
					searchTab.getParametersForm().validate();
					searchTab.getAdvancedParametersForm().validate();
					if (searchTab.getParametersForm().hasErrors() || searchTab.getAdvancedParametersForm().hasErrors()) {
						SC.warn("Some search parameters have invalid values.");
						return;
					} else {
						search(searchTab, mapPanel);
					}
				}
			}
			
		});
	}
	
	/**
	 * Get search parameters and invoke Requester.
	 * @param searchTab
	 */
	private void search(SearchTab searchTab, MapPanel mapPanel) {
		LatLngBounds bounds = mapPanel.getRoi().getBounds();
		LatLng northEast = bounds.getNorthEast();
		LatLng southWest = bounds.getSouthWest();
		String queryString = "zt=rectangle" +
				"&sd=" + getDateString((Date) searchTab.getStartDate().getValue()) +
				"&ed=" + getDateString((Date) searchTab.getEndDate().getValue()) +
				"&sn=Undefined" +
				"&nwlat=" + northEast.getLatitude() +
				"&nwlon=" + southWest.getLongitude() +
				"&selat=" + southWest.getLatitude() +
				"&selon=" + northEast.getLongitude() +
				"&mc=" + ((Integer) searchTab.getMaxCloud().getValue()).toString() +
				"&ms=100" +
				"&mi=" + ((Integer) searchTab.getMaxIncidence().getValue()).toString() +
				"&minr=" + (String) searchTab.getMinResolution().getValue() +
				"&maxr=" + (String) searchTab.getMaxResolution().getValue() +
				"&fullness=autoshift"; // +
				//"&sc=TT,KK";
		Requester.send(queryString, new SearchResultEvent(), true);
	}

	/**
	 * Get date string from {@link Date} for API.
	 * @param sd
	 * @return
	 */
	@SuppressWarnings("deprecation")
	private String getDateString(Date sd) {
		return (1900 + sd.getYear()) + "-" + (sd.getMonth()+1) + "-" + sd.getDate() + "T00:00:00";
	}

}
